package com.mooer.app;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mooer.MooerApplication;
import com.mooer.R;
import com.mooer.bean.SignAccount;
import com.mooer.bean.User;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;

public class SignMe extends Activity implements OnClickListener {

	private TextView name, uid, miaomiaomiao, maoliang, maoling, group,
			signTimes;

	private ImageView login, regist;

	private LinearLayout unLoginLayout, loginLayout, mySignLayout;

	private SignAccount account;
	
	private MooerApplication app;
	
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_me);
		loginLayout = (LinearLayout) findViewById(R.id.login);
		unLoginLayout = (LinearLayout) findViewById(R.id.unlogin);
		mySignLayout = (LinearLayout) findViewById(R.id.my_sign);
		login = (ImageView) findViewById(R.id.login_btn);
		regist = (ImageView) findViewById(R.id.regist_btn);
		name = (TextView) findViewById(R.id.name);
		uid = (TextView) findViewById(R.id.uid);
		miaomiaomiao = (TextView) findViewById(R.id.miaomiaomiao);
		maoliang = (TextView) findViewById(R.id.maoliang);
		maoling = (TextView) findViewById(R.id.maoling);
		group = (TextView) findViewById(R.id.group);
		signTimes = (TextView) findViewById(R.id.sign_times);

		uid.setText(getString(R.string.uid, "加载中..."));
		miaomiaomiao.setText(getString(R.string.miaomiaomiao, "加载中..."));
		maoliang.setText(getString(R.string.maoliang, "加载中..."));
		maoling.setText(getString(R.string.maoling, "加载中..."));
		group.setText(getString(R.string.group, "加载中..."));
		loginLayout.setVisibility(View.VISIBLE);
		unLoginLayout.setVisibility(View.GONE);
		mySignLayout.setOnClickListener(this);
		login.setOnClickListener(this);
		regist.setOnClickListener(this);
		app = (MooerApplication) getApplication();
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		user = app.getUser();
		if(!app.isLoggedin() || user == null || user.getId() < 1 ){
			Intent intent = new Intent(this, MoreAccountSetting.class);
			intent.putExtra("isFinish", true);
			startActivity(intent);
		}else if(account == null){
			new UserInfoTask().execute(user.getId());
		}
	}



	@Override
	public void onClick(View v) {
		if (v == mySignLayout) {
			Intent intent = new Intent(this, SignMeList.class);
			intent.putExtra("number", account.getSignCount());
			intent.putExtra("id", account.getUser().getId());
			startActivity(intent);
		}

	}

	private class UserInfoTask extends AsyncTask<Integer, Integer, SignAccount> {

		private boolean isException;

		@Override
		protected SignAccount doInBackground(Integer... params) {
			SignAccount account = null;
			try {
				account = HttpUtils.fetchAccountInfo(params[0]);
			} catch (Exception e) {
				isException = true;
				e.printStackTrace();
			}
			return account;
		}

		@Override
		protected void onPostExecute(SignAccount account) {
			super.onPostExecute(account);
			if (isException) {

			} else {
				SignMe.this.account = account;
				name.setText(account.getUser().getUserName());
				uid.setText(getString(R.string.uid, account.getUser().getId()));
				miaomiaomiao.setText(getString(R.string.miaomiaomiao,
						account.getSignCount()));
				maoliang.setText(getString(R.string.maoliang,
						account.getCatFood()));
				maoling.setText(getString(R.string.maoling, account.getGrade()));
				group.setText(getString(R.string.group, account.getGroupName()));
				signTimes.setText("(" + account.getSignCount() + ")");
			}
		}

	}

}
